<?php

/**
 * 用户列表
 *
 */
function & li($page_size = ADM_PAGESIZE) {
    global $_TABLES, $class_var;
    $view_var   = array();
    $tbl_zone   = $_TABLES['city'];
    $conn       = & get_db_conn();
    $where      = 'WHERE 1 ';
    // 关键字
    $key    = trim(@$_GET['key']);
    
    $num    = trim(@$_GET['num']);
    $num1    = trim(@$_GET['num1']);
    $num2    = trim(@$_GET['num2']);
    
    if(isset($_GET['num'])&&$num!=-1){
        $where  .= "AND (num='$num1' and num1='$num2') ";
    }
    
    if($key != '') $where  .= "AND (cname LIKE '%$key%') ";
    
    
    // 分页处理开始
    $csql = "SELECT COUNT(*) AS rs_count FROM $tbl_zone $where";
    $rs   = $conn->GetRow($csql);


    $record_total   = @(int)$rs['rs_count'];
    $page_total     = ceil($record_total/$page_size);

    $page   = @(int)$_GET['page'];
    if($page < 1) $page = 1;
    if($page > $page_total && $page_total > 0) $page = $page_total;
    $offset = ($page - 1) * $page_size;
    $limit  = "LIMIT $offset, $page_size";

    $view_var['record_total']   = $record_total;
    $view_var['page_total']     = $page_total;
    $view_var['page']           = $page;
    $view_var['page_size']      = $page_size;
    // 分页处理完毕
    $sql    = "SELECT * FROM $tbl_zone $where ORDER BY id desc ";
    $rs = $conn->PageExecute($sql,$page_size,$page);
    $view_var['record'] = $rs->GetRows();
    return $view_var;
}

function read($id = 0) {
    global $_TABLES, $timestamp, $G_big_integer;

    $view_var   = array();
    $tbl_main   = $_TABLES['city'];
    if($id > 0) {
        $conn   = & get_db_conn();
        $sql    = "SELECT * FROM $tbl_main WHERE id = $id ";
        $view_var   = $conn->GetRow($sql);
    } else {
        $view_var   = array(
            'id' => 0,
            'num' => '',
            'cname' => ''
            );
    }
    return $view_var;
}

function request() {
    $data = $_POST['zone'];
    $data['num']=@(int)$data['num1'];
    $data['num1']=@(int)$data['num2'];
    unset($data['num2']);
    return $data;
}

function not_samerecord($title, $id) {
    global $_TABLES;

    $tbl_main   = $_TABLES['city'];
    $conn       = & get_db_conn();
    $where='';
    if($id)$where=" and id<>$id ";
    $rs = $conn->GetRow("SELECT COUNT(*) AS rs_count FROM $tbl_main WHERE cname = '$title' $where");
    if($rs['rs_count'] > 0) {
        $GLOBALS['ERR']->AddError(_ERROR_BUSINESS, 
                "系统当前已经存在名称为<FONT COLOR=\"red\">{$title}</FONT>的记录，无法当前操作！", 
                __FILE__,
                __LINE__);
        return false;
    }
    return true;
}


function add() {
    global $_TABLES, $timestamp;
    $tbl_zone = $_TABLES['city'];
    $result = request();
    if($result !== false) {
        $conn   = & get_db_conn();
        unset($result['id']);
        if(not_samerecord($result['cname'],0)){
            if($conn->AutoExecute($tbl_zone, $result, 'INSERT')) {
                $id = $conn->Insert_ID();
                return $id;
            }
        }
    }
    return false;
}

function modify() {
    global $_TABLES, $timestamp, $mod;

    $tbl_zone = $_TABLES['city'];
    $result = request();
    if($result !== false) {
        $conn   = & get_db_conn();
        if(not_samerecord($result['cname'], $result['id'])){
            if($conn->AutoExecute($tbl_zone, $result,'UPDATE', 'id=' . $result['id'] )) {
                return $result['id'];
            }
        }
    }
    return false;
}



function del() {
    global $_TABLES, $mod, $save_flag;
    $tbl_zone = $_TABLES['city'];

    $deltype    = @(int)$_POST['deltype'];
    if($deltype == $save_flag) {
        $str_in = array_join_comma($_POST['item_id']);
    } else {
        $item_id= @(int)$_GET['item_id'];
        $str_in = $item_id;
    }
    if($str_in != '') {
        $conn   = & get_db_conn();
        // 删除纪录
        $sql    = "DELETE FROM $tbl_zone 
                    WHERE id IN ({$str_in}) ";
        if($conn->Execute($sql)) {
            js_success('删除指定记录成功！');
        }
    }
}
?>